import { GameData } from "../../common/GameConfig";
import { fishTypeView } from "./pfSelecFish";

const { ccclass, property } = cc._decorator;

@ccclass
export default class pfUnlock extends cc.Component {
  @property(cc.Label)
  txtFishName: cc.Label = null;
  @property(cc.Label)
  txtTime: cc.Label = null;
  @property(cc.Label)
  txtAward: cc.Label = null;
  @property(cc.Sprite)
  spFish: cc.Sprite = null;
  @property(cc.Label)
  txtUnlockMoney: cc.Label = null;
  @property(cc.Label)
  txtShengMoney: cc.Label = null;
  @property(cc.Label)
  txtLockAward: cc.Label = null;
  _ID = 0;

  setViewData(data) {
    if (!data) return;

    this.spFish.spriteFrame = data.spFish;
    this.txtFishName.string = data.name;
    this.txtTime.string = data.time;
    this.txtAward.string = data.profit;
    this.txtLockAward.string = data.reward;
    this.txtUnlockMoney.string = data.money;
    this.txtShengMoney.string = "立省" + data.money;
    this._ID = data.id;
  }

  onButtonUnlockFishCAllback() {
    if (GameData.money >= ~~this.txtUnlockMoney.string) {
      GameData.money -= ~~this.txtUnlockMoney.string;
      this.unlockFish();
    } else {
      console.log("can not unlock fish");
    }
  }

  unlockFish() {
    GameData.fish_list.push(this._ID);
    fishTypeView.createOrUpDateFishItem();
    this.node.removeFromParent();
    this.node.destroy();
  }

  onButtonCloseCallback() {
    this.node.removeFromParent();
    this.node.destroy();
  }

  onButtonVideoUnlockFishCallback() {
    console.log("show  video  lock fish");
    let call = (state) => {
      if (!state) return;
      this.unlockFish();
    };

    call(true);
  }
}
